import {Menus, Tabs} from "webextension-polyfill";
import ContextType = Menus.ContextType;
import OnClickData = Menus.OnClickData;
import CreateCreatePropertiesType = Menus.CreateCreatePropertiesType;
import Tab = Tabs.Tab;
import {useSetNotifications} from "@/utils/setNotifications";

export function useSetMenu() {

    browser.contextMenus.onClicked.addListener(genericOnClick);

    // 一个通用的 onclick 回调函数。
    function genericOnClick(info: OnClickData, tab: Tab) {
        switch (info.menuItemId) {
            case 'radio':
                // Radio item function
                console.log('Radio item clicked. Status:', info.checked);
                break;
            case 'checkbox':
                // Checkbox item function
                console.log('Checkbox item clicked. Status:', info.checked);
                break;
            case 'child1':
                console.log('notifications');
                useSetNotifications()
                break;
            default:
                // Standard context menu item function
                console.log('Standard context menu item clicked.');
        }
    }

    browser.runtime.onInstalled.addListener(function () {


        // Create one test item for each context type.
        let contexts: ContextType[] = [
            'page',
            'selection',
            'link',
            'editable',
            'image',
            'video',
            'audio'
        ];
        for (let i = 0; i < contexts.length; i++) {
            let context = contexts[i];
            let title = "Test '" + context + "' menu item";
            browser.contextMenus.create<CreateCreatePropertiesType>({
                title: title,
                contexts: [context],
                id: context
            });
        }

        // Create a parent item and two children.
        let parent = browser.contextMenus.create({
            title: 'Test parent item',
            id: 'parent'
        });
        browser.contextMenus.create({
            title: 'Child 1',
            parentId: parent,
            id: 'child1'
        });
        browser.contextMenus.create({
            type: 'separator',
            parentId: parent,
            id: 'separator',

        });

        let child2 = browser.contextMenus.create({
            title: 'Child 2',
            parentId: parent,
            id: 'child2',
        } as CreateCreatePropertiesType);
        browser.contextMenus.create({
            title: 'child2-1',
            parentId: child2,
            id: 'child2-1'
        });
        // Create a radio item.
        browser.contextMenus.create({
            title: 'radio',
            type: 'radio',
            id: 'radio',
        });
        browser.contextMenus.create({
            title: 'radio1',
            type: 'radio',
            id: 'radio1',
        });


        // Create a checkbox item.
        browser.contextMenus.create({
            title: 'checkbox',
            type: 'checkbox',
            id: 'checkbox',

        });
        // radio 自动为一组
        browser.contextMenus.create({
            title: 'radio',
            type: 'radio',
            id: 'radio2',
        });
        browser.contextMenus.create({
            title: 'radio1',
            type: 'radio',
            id: 'radio3',
        });

        browser.contextMenus.create(
            {title: 'Oops', parentId: 999, id: 'errorItem'},
            function () {
                if (browser.runtime.lastError) {
                    console.log('Got expected error: ' + browser.runtime.lastError.message);
                }
            }
        );
    });
}